package com.lk.action.json;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.opensymphony.xwork2.ActionSupport;


public class BaseAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{

	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private HttpServletResponse response;
	private Map<String,Object> session;
	
	//HttpServletRequest request = ServletActionContext.getRequest();
	//HttpServletResponse response = ServletActionContext.getResponse();
	
	public HttpServletRequest getRequest() {
		return request;
	}
	public HttpServletResponse getResponse() {
		return response;
	}
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	public Map<String, Object> getSession() {
		return session;
	}
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	public Gson getGson(){
		GsonBuilder gb=new GsonBuilder() ;  
		gb.excludeFieldsWithoutExposeAnnotation();
		Gson gson = gb.create();
		return gson;
	}
	
	public void formatJson(String jsondata,String formatdata) throws IOException{
		jsondata = getGson().toJson(formatdata);
		getResponse().setContentType("application/json,charset=UTF-8");
		PrintWriter out = getResponse().getWriter();
		out.print(jsondata);
		out.flush();
		out.close();
	}
	
}
